package com.rms.hkpc.presenter;


import com.google.gson.reflect.TypeToken;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.db.dto.ShortDto;
import com.rms.hkpc.db.dto.UpdateWorkOrderDto;
import com.rms.hkpc.db.dto.WorkOrderPhotosDto;
import com.rms.hkpc.db.dto.WorksOrderDto;
import com.rms.hkpc.model.AddHocModel;
import com.rms.hkpc.model.ComboModel;
import com.rms.hkpc.model.RecordsDetailModel;
import com.rms.hkpc.model.UtilModel;
import com.rms.hkpc.model.listener.OnResultListener;
import com.rms.hkpc.util.OKHttpUtil;
import com.rms.hkpc.util.SPUtil;
import com.rms.hkpc.view.IAddHocView;

import java.util.List;

public class AddHocPresenter extends BasePresenter<IAddHocView> {
    private RecordsDetailModel recordsDetailModel = new RecordsDetailModel();
    private AddHocModel addHocModel = new AddHocModel();
    private UtilModel utilModel = new UtilModel();
    private ComboModel comboModel = new ComboModel();

    public void getRecords(String id) {
        getView().showLoading();
        recordsDetailModel.getRecords("/api/esp/workOrders?id=" + id, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetDataSuccess((ApiResultData<WorksOrderDto>) apiResult);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetDataFails(e);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<WorksOrderDto>>() {
        }.getType());
    }

    public void upload(String path, final int pos) {
        getView().showLoading();
        utilModel.upload(path, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onUploadSuccess((ApiResultData<String>) apiResult, pos);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onUploadFails(e, pos);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }


    public void update() {
        getView().showLoading();
        addHocModel.update(getBean(), new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onUpdateSuccess((ApiResultData<String>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onUpdateFails(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void combo(String typeKey) {
        getView().showLoading();
        comboModel.combo(typeKey, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetComboSuccess((ApiResultData<List<ShortDto>>) apiResult);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetComboFails(e);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }

    public void create() {
        getView().showLoading();
        addHocModel.create(getBean(), new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onCreateSuccess((ApiResultData<String>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onCreateFails(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    private UpdateWorkOrderDto getBean() {
        UpdateWorkOrderDto bean = new UpdateWorkOrderDto();
        bean.setId(getView().getId());
        bean.setProjId(SPUtil.getInstance().getString(AllConstants.PROJECT_ID));
        bean.setStatusId(getView().getStatusId());
        bean.setParamId(getView().getParamId());
        bean.setVoice(getView().getVoice());
        bean.setVal(getView().getVal());
        bean.setResult(getView().getResult());
        bean.setExpired(getView().getExpDays());
        bean.setRemarks(getView().getRemarks());
        bean.setExpAt(getView().getExpAt());
        bean.setwAt(getView().getWAt());
        List<WorkOrderPhotosDto> photos = getView().getPhotos();
        if (photos != null && photos.size() > 0) {
            for (int i = 0; i < photos.size(); i++) {
                if (photos.get(i).getPhoto().contains("http")) {
                    photos.get(i).setPhoto(photos.get(i).getPhoto().substring(OKHttpUtil.IMG_URL.length()));
                }
            }
        }
        bean.setPhotos(photos);
        return bean;
    }
}
